<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>cometd-yui3 demo</title>
    <script src="../cometd-yui3/cometd-yui3-loader.js" type="text/javascript"></script>
    <style type="text/css">
        tr.failure {
            background-color: lightpink
        }
    </style>
</head>
<body>

<form>
    <input type="button" id="handshake" value="handshake" alt="notice that handshake will trigger 'connect' by advice">
    <input type="button" id="connect" value="connect">&nbsp;&nbsp;
    <input type="button" id="subscribe" value="subscribe(/test/channel0)">
    <input type="button" id="subscribeWithMessage" value="Subscribe with a Test Message">
    <input type="button" id="publish" value="Publish a Test Message">
    <input type="button" id="disconnect" value="disconnect"> (After disconnect, the client will take the server advice
    to reconnect immediately)
    <br>
    <input type="button" id="subscribeToChat" value="subscribe(/chat/demo)">
    <input type="button" id="unload" value="unload">
    <input type="button" id="sessionId" value="SessionId">

</form>
<hr>
<div id="container">
    <table border="1" cellspacing="0" width="100%">
        <thead>
        <tr>
            <th>Msg ID</th>
            <th>Successful?</th>
            <th>Response body</th>
        </tr>
        </thead>
    </table>
</div>


<script type="text/javascript">
    comet.YUI({base:'../yui3/',cometBase:'../cometd-yui3/'}).use('comet', function(Y) {
        var comet = new Y.Comet({uri:'.cometd'});
        var addRowCallback = function(evt) {
            try {
                var resp = (evt && evt.details && evt.details.length > 0) ? evt.details[0] : null;
                //Y.log("addRowCallback().  resp:" + Y.JSON.stringify(resp), "info", "page");
                if (resp) {
                    var row = Y.Node.create('<tr class="' + (!!resp.successful ? 'success' : 'failure') + '"><td>' + resp.id +
                                            '</td><td>' + resp.successful + '</td><td>' + Y.JSON.stringify(resp) + "</td></tr>");
                    Y.Node.get('#container table').appendChild(row);
                }
            } catch(e) {
                alert('exception in subscriber: ' + e.message)
            }
        }
        comet.on('incoming', addRowCallback);

        var addRowEventMessageCallback = function(data, msg) {
            try {
                if (data) {
                    var row = Y.Node.create('<tr class="success"><td>' +
                                            '</td><td>' + 'message' + '</td><td>' + Y.JSON.stringify(data) + "</td></tr>");
                    Y.Node.get('#container table').appendChild(row);
                }
            } catch(e) {
                alert('exception in subscriber: ' + e.message)
            }
        }

        var messageHandler = function(message) {
            Y.log('meta msg - message:' + Y.JSON.stringify(message), 'info', 'comet');
        }
        var eventMessageHandler = function(data, msg) {
            Y.log('even msg - message:' + Y.JSON.stringify(data), 'info', 'comet');
        }

        Y.on("click", function() { comet.handshake({'token':'secret'}); }, "#handshake");
        Y.on("click", function() { comet.connect(); }, "#connect");
        Y.on("click", function() { comet.subscribe({channel:'/chat/**'}); }, "#subscribe");
        Y.on("click", function() { comet.subscribe({channel:'/chat/**', listener:addRowEventMessageCallback}, {channel:'/chat/channel0', data:{hello:'world'}}); }, "#subscribeWithMessage");
        //Y.on("click", function() { comet.send({channel:'/chat/channel0', data:{hello:'world'}}); }, "#publish");
        Y.on("click", function() { comet.disconnect(); }, "#disconnect");
        Y.on("click", function() { comet.handleBeforeUnload(); }, "#unload");

        Y.on("click", function() { comet.subscribe({channel:'/chat/demo', listener:eventMessageHandler}, {data:{user:'a',join:true,chat:'hello'}}); }, "#subscribeToChat");

        Y.on("click", function() { alert(new YUI().use('cookie').Cookie.get('JSESSIONID')) }, "#sessionId");


    });
</script>

</body>
</html>